package com.cskaoyan.service;

import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description:
 * @author:刘杰
 * @create: 2022-08-13 14:15
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {


    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData list() {


        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();

        criteria1.andPidEqualTo(0)
                .andDeletedEqualTo(false);
        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
        for (MarketCategory marketCategory : marketCategories1) {
            MarketCategoryExample marketCategoryExample2 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria2 = marketCategoryExample2.createCriteria();
            criteria2.andPidEqualTo(marketCategory.getId())
                    .andDeletedEqualTo(false);
            List<MarketCategory> marketCategories2 = marketCategoryMapper.selectByExample(marketCategoryExample2);
            marketCategory.setChildren(marketCategories2);
        }

        criteria1.andDeletedEqualTo(false);
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategories1);
        CommonData data = CommonData.data(pageInfo);
        return data;

    }

    @Override
    public void update(MarketCategory marketCategory) {
        marketCategory.setUpdateTime(new Date());
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        Date date = new Date();
        marketCategory.setAddTime(date);
        marketCategory.setUpdateTime(date);
        marketCategory.setDeleted(false);
        marketCategory.setSortOrder((byte) 0);
        marketCategoryMapper.insertSelective(marketCategory);
        MarketCategory marketCategoryVo = marketCategoryMapper.selectByPrimaryKey(marketCategory.getId());
        return marketCategoryVo;
    }

    @Override
    public void delete(MarketCategory marketCategory) {
        MarketCategory marketCategory1 = new MarketCategory();
        marketCategory1.setId(marketCategory.getId());
        marketCategory1.setDeleted(true);
        marketCategory1.setUpdateTime(new Date());
        //update
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory1);
    }

    @Override
    public CommonData l1() {
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();

        criteria1.andPidEqualTo(0)
                .andDeletedEqualTo(false);

        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
        List<MarketCategoryL1> marketCategoryL1s = new LinkedList<>();
        for (MarketCategory marketCategory : marketCategories1) {
            marketCategoryL1s.add(new MarketCategoryL1(marketCategory.getId(), marketCategory.getName()));
        }
        PageInfo<MarketCategory> pageInfo = new PageInfo<>(marketCategories1);
        CommonData data = CommonData.data(pageInfo);
        return data;

    }
}
